﻿<%@ WebHandler Language="C#" Class="User" %>

using System;
using System.Web;
using Company.StudyClub.BLL.Encrypt;
using Company.StudyClub.BLL.Common;
using Company.StudyClub.BLL.User;
using Company.StudyClub.BLL.Site;
using System.Collections.Generic;
using System.Text;
using Company.StudyClub.Model;

public class User : IHttpHandler {
    private HttpHelper helper = new HttpHelper();
    public void ProcessRequest (HttpContext context) {
        HttpResponse response = context.Response;
        switch(helper.GetString("type"))
        {
            case "login":
                DoLogin(response);
                break;
            case "usersnum":                
                GetTotalUsers(response);
                break;
            case "searchuser":
                SearchUsers(response);
                break;
            case "checkaccount":
                CheckAccount(response);
                break;
            case "checkemail":
                CheckEmail(response);
                break;
            case "adduser":
                AddUser(response);
                break;
            case "getuser":
                GetUser(response);
                break;
            case "edituser":
                EditUser(response);
                break;
            case "deleteuser":
                DeleteUsers(response);
                break;
            case "logout":
                DoLogout(response);
                break;
        }
    }
    /// <summary>
    /// 登陆
    /// </summary>
    /// <param name="respose"></param>
    private void DoLogin(HttpResponse response)
    {
        UserBLL objBLL = new UserBLL();
        User_Info model = new User_Info();
        string account = helper.GetString("name").Trim();
        string pwd = helper.GetString("pwd").Trim();
        model = objBLL.GetModel(account, EncryptLayer.En(pwd));
        if (model != null && model.Status == 1 && model.UserType==UserType.MANAGER)
        {
            string user = objBLL.SerialUser(model);
            objBLL.WriteSerialUserCookie(user);
            response.Write("1");
        }
        else
        {
            response.Write("0");
        }        
    }

    private void DoLogout(HttpResponse response)
    {
        try
        {
            UserBLL objBLL = new UserBLL();
            objBLL.ClearSerialUserCookie();

            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }        
    }

    private void GetTotalUsers(HttpResponse response)
    {
        string account = helper.GetString("account");
        string nickname = helper.GetString("nickname");
        string realname = helper.GetString("realname");
        string email = helper.GetString("email");

        UserBLL objBLL = new UserBLL();

        response.Write(objBLL.GetTotalUsers(0, account, nickname, realname, email));
    }

    private void SearchUsers(HttpResponse response)
    {
        int pageIndex = helper.GetInt("pageindex");
        int pageSize = helper.GetInt("pagesize");
        string account = helper.GetString("account");
        string nickname = helper.GetString("nickname");
        string realname = helper.GetString("realname");
        string email = helper.GetString("email");
        
        UserBLL objBLL = new UserBLL();

        List<User_Info> objList = objBLL.SearchUsers(pageIndex,pageSize,0,account,nickname,realname,email);

        StringBuilder objSB = new StringBuilder();

        objSB.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            User_Info model = (User_Info)objList[i];
            objSB.Append("{\"uid\":\"");
            objSB.Append(model.UserID);
            objSB.Append("\",\"account\":\"");
            objSB.Append(model.Account);
            objSB.Append("\",\"nickname\":\"");
            objSB.Append(model.NickName);
            objSB.Append("\",\"realname\":\"");
            objSB.Append(model.RealName);
            objSB.Append("\",\"email\":\"");
            objSB.Append(model.Email);
            objSB.Append("\",\"row\":\"");
            objSB.Append(model.RowNumber);
            objSB.Append("\"}");

            if (i != objList.Count - 1)
                objSB.Append(",");
        }
        objSB.Append("]");
        
        response.Write(objSB.ToString());
    }

    private void CheckAccount(HttpResponse response)
    {
        string curaccount = helper.GetString("curaccount");
        string account = helper.GetString("account");
        if (curaccount.Length > 0)
        {
            if (account == curaccount)
            {
                response.Write("true");
                return;
            }
        }
        
        UserBLL objBLL = new UserBLL();
        string result = "";

        if (objBLL.CheckAccountExists(account))
            result = "false";
        else
            result = "true";
        
        response.Write(result);
    }

    private void CheckEmail(HttpResponse response)
    {
        string curemail = helper.GetString("curemail");
        string email = helper.GetString("email");
        if (curemail.Length > 0)
        {
            if (email == curemail)
            {
                response.Write("true");
                return;
            }
        }
        
        UserBLL objBLL = new UserBLL();
        string result = "";
        if(objBLL.CheckEmailExists(email))
            result = "false";
        else
            result = "true";
        
        response.Write(result);
    }

    private void AddUser(HttpResponse response)
    {     
        User_Info model = new User_Info();
        model.Account = helper.GetString("account");
        model.Address = helper.GetString("address");
        model.Gender = helper.GetInt("gender");
        model.IDCard = helper.GetString("idcard");
        model.NickName = helper.GetString("nickname");
        model.Password = EncryptLayer.En(helper.GetString("pwd"));
        model.PhoneNumber = helper.GetString("phonenumber");
        model.RealName = helper.GetString("realname");
        model.Email = helper.GetString("email");
        model.RegDate = DateTime.Now;
        model.Status = 1;
        model.RegionID = helper.GetInt("regionid");
        model.SchoolID = helper.GetInt("schoolid");
        model.CellPhoneNumber = helper.GetString("cellphone");
        model.UserType = helper.GetInt("usertype");
        string birthday = helper.GetString("birthday");
        if (birthday.Length == 0)
            birthday = "1900-1-1";
        model.Birthday = DateTime.Parse(birthday);
        
        UserBLL objBLL = new UserBLL();
        int uid = objBLL.Add(model);

        response.Write(uid.ToString());
    }

    private void GetUser(HttpResponse response)
    {
        UserBLL objBLL = new UserBLL();

        User_Info model = objBLL.GetModel(helper.GetInt("id"));

        if (model != null)
        {
            StringBuilder objSb = new StringBuilder();

            objSb.Append("{\"uid\":\"");
            objSb.Append(model.UserID);
            objSb.Append("\",\"realname\":\"");
            objSb.Append(model.RealName);
            objSb.Append("\",\"nickname\":\"");
            objSb.Append(model.NickName);
            objSb.Append("\",\"account\":\"");
            objSb.Append(model.Account);
            objSb.Append("\",\"email\":\"");
            objSb.Append(model.Email);
            objSb.Append("\",\"cellphone\":\"");
            objSb.Append(model.CellPhoneNumber);
            objSb.Append("\",\"usertype\":\"");
            objSb.Append(model.UserType);
            objSb.Append("\",\"gender\":\"");
            objSb.Append(model.Gender);
            objSb.Append("\",\"birthday\":\"");
            objSb.Append(model.Birthday.ToString("yyyy年MM月dd日"));
            objSb.Append("\",\"phone\":\"");
            objSb.Append(model.PhoneNumber);
            objSb.Append("\",\"address\":\"");
            objSb.Append(model.Address);
            objSb.Append("\",\"schoolid\":\"");
            objSb.Append(model.SchoolID);
            objSb.Append("\"}");

            response.Write(objSb.ToString());
        }
        else
        {
            response.Write("{}");
        }
    }

    private void EditUser(HttpResponse response)
    {
        User_Info model = new User_Info();
        model.UserID = helper.GetInt("id");
        model.Account = helper.GetString("account");
        model.Address = helper.GetString("address");
        model.Gender = helper.GetInt("gender");
        model.IDCard = helper.GetString("idcard");
        model.NickName = helper.GetString("nickname");    
        model.PhoneNumber = helper.GetString("phonenumber");
        model.RealName = helper.GetString("realname");
        model.Email = helper.GetString("email");
        model.RegDate = DateTime.Now;
        model.Status = 1;
        model.RegionID = helper.GetInt("regionid");
        model.SchoolID = helper.GetInt("schoolid");
        model.CellPhoneNumber = helper.GetString("cellphone");
        model.UserType = helper.GetInt("usertype");
        
        string birthday = helper.GetString("birthday");
        if (birthday.Length == 0)
            birthday = "1900-1-1";
        model.Birthday = DateTime.Parse(birthday);

        string password = helper.GetString("pwd");
        if (password.Length == 0)
            model.Password = null;
        else
            model.Password = EncryptLayer.En(password);
        
        UserBLL objBLL = new UserBLL();
        objBLL.Update(model);

        response.Write("1");
    }

    private void DeleteUsers(HttpResponse response)
    {
        try
        {
            string[] uids = helper.GetString("uids").Split(',');
            
            for (int i = 0; i < uids.Length; i++)
            {
                if (uids[i] == string.Empty)
                    continue;
                    
                UserBLL objBLL = new UserBLL();
                objBLL.Delete(int.Parse(uids[i]));
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }    
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}